-module(vendor_service).
-include("rec_dec.hrl").
-include_lib("stdlib/include/qlc.hrl").
-export([add/1,get/0,get/1,del/1,start/0,addchange/1,delete/1]).
-define(TAB    , vendor_rec).
-define(CFG_TAB, vendor_cfg).
-define(U_V_REL_TAB, user_vendor_rec). 

addchange(Rec) when is_record(Rec,?TAB) ->
	case Rec#?TAB.id of
	"" ->
		add(Rec);
	_ ->
		modify(Rec)
	end;
addchange(Rec) when is_record(Rec,?CFG_TAB) ->
	db_mgr:write(Rec).
	
add(Rec) when is_record(Rec,?TAB) ->
	Id = gen_server:call(couch_uuids,create),
	V = db_mgr:write(?TAB,Rec#?TAB{id=Id}),
	user_service:addchange(#user_vendor_rec{
			v_id = Id,
			u_id = V#vendor_rec.register_id,
			created_date = calendar:local_time()
		}),
	V.
modify(Rec) when is_record(Rec,?TAB) ->
	db_mgr:write(?TAB,Rec).
	
get() -> %% get all vendor info
	db_mgr:get(?TAB).
	
get({short_name,SN}) ->
	Qry = qlc:q([X||X <- mnesia:table(?TAB),X#?TAB.short_name =:= SN]),
	db_mgr:do_transaction(fun() -> qlc:e(Qry) end);
	
get({register,RegisterId}) ->
	Qry = qlc:q([X||X <- mnesia:table(?TAB),X#?TAB.register_id =:= RegisterId]),
	db_mgr:do_transaction(fun() -> qlc:e(Qry) end);

get({managedby_only,Uid,admin}) ->
	db_mgr:do_transaction(fun() -> qlc:e(qlc:q([{X#vendor_rec.id,X,true} ||
												X <- mnesia:table(?TAB), 
												(Uid =:= X#vendor_rec.register_id)]))
										 end);

get({managedby_only,Uid,operator}) ->
	db_mgr:do_transaction(fun() -> qlc:e(qlc:q([{X#vendor_rec.id,X,true} ||
												X <- mnesia:table(?TAB),Y <-mnesia:table(?U_V_REL_TAB), 
												(Uid =:= Y#user_vendor_rec.u_id) or (Uid =:= X#vendor_rec.register_id),
												(X#vendor_rec.id =:= Y#user_vendor_rec.v_id) or (Uid =:= X#vendor_rec.register_id)]))
										 end);
get({managedby_only,Uid}) ->
	case user_service:get({id,Uid}) of
	[U|_] ->
		vendor_service:get({managedby_only,Uid,U#user_rec.right});
	[] ->
		[]
	end;
										 
get({managedby,Uid}) ->
	case user_service:get({id,Uid}) of
	[] ->
		[];
	[U|_] ->
		AllVendors = vendor_service:get({register,U#user_rec.register_id}),
		ManageV = vendor_service:get({managedby_only,Uid}),
		lists:map(
			fun(E) ->
				case lists:keyfind(E#vendor_rec.id,1,ManageV) of
				{_,E,S} ->
					{E,S};
				_ ->
					{E,false}
				end
			end,
			AllVendors)
	end;	
get({cfg,Id}) ->
	case db_mgr:get(?CFG_TAB,Id) of
	[Res|_] ->
		Res;
	_ ->
		#?CFG_TAB{id=Id,pre_out_notify_cond = 0}
	end;
	
	
get(Id) when is_binary(Id) ->
	case db_mgr:get(?TAB,Id) of
	[Res|_] ->
		Res;
	_ ->
		#?TAB{name="missed"}
	end;
get(Id) when is_list(Id) ->
	vendor_service:get(list_to_binary(Id)).

delete(Id) when is_binary(Id) ->
	lists:foreach(fun(E) ->
					queue_service:delete(E#queue_rec.id)
				  end,
				  queue_service:get({vendor_id,Id})),
	lists:foreach(fun(E) ->
					user_service:delete(E#user_rec.id)
				  end,
				  user_service:get({vendor_id,Id})),
	del(Id).
start() ->
	case db_mgr:check_table(?TAB) of
	no_found ->
		db_mgr:create_table(?TAB,record_info(fields,?TAB));
	_ ->
		ok
	end,
	case db_mgr:check_table(?CFG_TAB) of
	no_found ->
		db_mgr:create_table(?CFG_TAB,record_info(fields,?CFG_TAB));
	_ ->
		ok
	end.
del(ID) ->
	db_mgr:del(?TAB,ID).